<template>
  <el-container>
    <el-aside style="width: var(--nav-bar-width);">
      <h5>天机音乐</h5>
      <el-menu
        default-active="1"
        ref="navMenu"
        class="nav-menu"
        @select="nameMenuSelected"
        unique-opened
      >
        <el-menu-item index="1">
          <i class="ri-home-line ri-2x"></i>
          <span slot="title">主页</span>
        </el-menu-item>
        <el-menu-item index="2">
          <i class="ri-play-list-line ri-2x"></i>
          <span slot="title">播放列表</span>
        </el-menu-item>
      </el-menu>
      <span class="version">Ver:20200602</span>
    </el-aside>

    <el-container>
      <el-header>
        <search ref="search"/>
        <div class="system-buttons">
          <span title="最小化" class="sys-btn" @click="MinimizeWindow">
            <svg width="20" height="18">
              <use href="~@/assets/window.svg#最小化" />
            </svg>
          </span>
          <span title="最大化" class="sys-btn" id="winmax" @click="Maximizewindow">
            <svg width="20" height="18">
              <use href="~@/assets/window.svg#最大化" />
            </svg>
          </span>
          <span title="关闭" class="sys-btn" @click="CloseWindow">
            <svg width="20" height="18">
              <use href="~@/assets/window.svg#关闭" />
            </svg>
          </span>
        </div>
      </el-header>

      <el-main class="main-view">
        <div id="cView">
          <subMain />
          <playList style="display:none;" ref="playList" />
        </div>
      </el-main>
    </el-container>

    <el-footer>
      <aplayer id="play-bar" autoplay :audio="currentMusic" ref="ap" @ended="onEnded" />
    </el-footer>
  </el-container>
</template>

<script>
import { ipcRenderer } from "electron";
import $ from "jquery";

import search from "./Search";
import subMain from "./SubPage/Main";
import playList from "./SubPage/PlayList";

var _this;
export default {
  name: "main-page",
  data() {
    return {
      currentMusic: {
        name: "天机音乐",
        artist: "tisn"
      }
    };
  },
  created: function() {
    _this = this;
    ipcRenderer.on("window-state", (event, args) => {
      var state = args;
      var btn = $("#winmax");
      if (state == "maximize") {
        btn.attr("title", "还原");
        btn
          .children("svg")
          .children("use")
          .attr("href", require("@/assets/window.svg") + "#还原");
      } else {
        btn.attr("title", "最大化");
        btn
          .children("svg")
          .children("use")
          .attr("href", require("@/assets/window.svg") + "#最大化");
      }
    });
    $(document).ready(function() {
      _this.$refs.search.parent = _this;
      _this.$refs.playList.parent = _this;
    });
  },
  methods: {
    CloseWindow() {
      ipcRenderer.send("window-state", "CloseWindow");
    },
    Maximizewindow() {
      ipcRenderer.send("window-state", "Maximizewindow");
    },
    MinimizeWindow() {
      ipcRenderer.send("window-state", "MinimizeWindow");
    },
    UpdateList(res) {
      this.HideAllSub();
      this.$refs.navMenu.activeIndex = "2";
      this.ShowSub(2);
      this.$refs.playList.UpdateList(res);
    },
    HideAllSub() {
      $("#cView")
        .children()
        .hide();
    },
    ShowSub(index) {
      $(`#cView > div:nth-child(${index})`).show();
    },
    nameMenuSelected() {
      this.HideAllSub();
      this.ShowSub(this.$refs.navMenu.activeIndex);
    },
    onEnded(e) {
      this.$refs.playList.Next();
    }
  },
  components: {
    subMain,
    playList,
    search
  }
};
</script>

<style>
:root {
  --nav-bar-width: 180px;
  --title-bar-height: 65px;
  --win-padding: 30px;
  --player-height: 66px;
}
.nav-bar {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--nav-bar-width);
  background: #0048ff10;
  overflow-y: auto;
  overflow-x: hidden;
  user-select: none;
  box-shadow: inset 0 1px 5px 0 rgba(212, 212, 233, 0.7);
  padding-bottom: var(--player-height);
}
.nav-logo {
  -webkit-app-region: drag;
  height: var(--title-bar-height);
}
.nav-menu {
  background: transparent;
}
.el-menu-item i {
  vertical-align: middle;
  padding-right: 15px;
}
.el-menu-item span {
  font-size: 16px;
}
h5 {
  text-align: center;
  font-weight: normal;
  font-size: 16px;
}
.title-bar {
  -webkit-app-region: drag;
  position: fixed;
  top: 0;
  left: var(--nav-bar-width);
  right: 0;
  height: var(--title-bar-height);
  background: #fefefe;
  box-shadow: 2px 0px 5px rgba(212, 212, 233, 0.7);
  z-index: 999;
}
.system-buttons {
  float: right;
  padding-right: var(--win-padding);
  padding-top: var(--win-padding);
}
.sys-btn {
  -webkit-app-region: no-drag;
  cursor: pointer;
  fill: #7c7c7c;
  padding-left: 8px;
}
.sys-btn :hover {
  fill: #1ecc94;
}
.main-view {
  padding: var(--title-bar-height) var(--win-padding) var(--win-padding)
    var(--nav-bar-width);
}
.main-view-s {
  padding: var(--win-padding) 0 var(--player-height) var(--win-padding);
}

.version {
  color: #aaa;
  padding-left: var(--win-padding);
  position: fixed;
  bottom: 88px;
}

::-webkit-scrollbar {
  width: 3px;
  height: 15px;
  background-color: #dbdbdb;
}

::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background-color: #aeaeae;
}
::-webkit-scrollbar-thumb:hover {
  background-color: #696969;
}
::-webkit-scrollbar-thumb:active {
  background-color: #3f3f3f;
}

::-webkit-scrollbar-track {
  background-color: #dbdbdb;
}

#play-bar {
  overflow: visible;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  margin: 0;
  background: #f4f9fc;
  box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.1);
  height: var(--player-height);
}

.el-header {
  -webkit-app-region: drag;
  position: fixed;
  top: 0;
  left: var(--nav-bar-width);
  right: 0;
  height: var(--title-bar-height);
  background: #fefefe;
  box-shadow: 2px 0px 5px rgba(212, 212, 233, 0.7);
  z-index: 999;
  padding: 0;
}

.el-aside {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--nav-bar-width);
  background: #0048ff10;
  overflow-y: auto;
  overflow-x: hidden;
  user-select: none;
  box-shadow: inset 0 1px 5px 0 rgba(212, 212, 233, 0.7);
  padding-bottom: var(--player-height);
}
h5{
  -webkit-app-region: drag;
  font-size: 20px;
}
</style>